﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NuGet.Packaging.Signing;
using WMS_MAV.DTO.Product;

namespace WMS_MAV.Controllers
{
    public class ProductController : Controller
    {
        private readonly IConfiguration _configuration;
        public ProductController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 产品管理展示
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowIndex()
        {
            return View();
        }
        /// <summary>
        /// 添加产品管理
        /// </summary>
        /// <returns></returns>
        public IActionResult AddProduct()
        {
            return View();
        }
        /// <summary>
        /// 产品类别
        /// </summary>
        /// <returns></returns>
        public IActionResult ProductTypeView()
        {
            return View();
        }
        public IActionResult add()
        {
            return View();
        }

        /// <summary>
        /// 批量反填产品数据
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetProductRangeModel(string token, string productId)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "productRead/api/ProductSelect/FanRangeProductInfo";
            //string list = JsonConvert.SerializeObject(productId.productId);
            var list = "{\"productId\":" + productId + "}";
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        ///// <summary>
        ///// 批量反填产品数据
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<ActionResult> GetProductRangeModel(string token, int productId)
        //{
        //    HttpClientWrapper client = new HttpClientWrapper(token);
        //    string url = "http://10.223.8.18:8000/productRead/api/ProductSelect/FanRangeProductInfo?productId=" + productId;
        //    var response = await client.GetAsync(url);
        //    return Json(response);
        //}
       
        
        /// <summary>
        /// 产品添加
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GEtProductAdd(string data)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/Product/AddProduct";
            var respomse = await client.PostAsync(url,data);
            return Json(respomse);
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GEtProductUpdate(PeoductDTO obj1,string token)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/Product/UpProduct";
            string list=JsonConvert.SerializeObject(obj1);
            var respomse = await client.PostAsync(url, list);
            return Json(respomse);
        }

        //产品管理显示
        [HttpGet]
        public async Task<ActionResult> GEtProductShow(string token, int index, int size, string? productNo, string? productName, string? manuNo, string? vendorId)
        {
           
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "productRead/api/ProductSelect/GetProductInfos?index=" + index + "&size=" + size + "&productNo=" + productNo +
                "&productName=" + productName + "&manuNo=" + manuNo + "&vendorId=" + vendorId;
            var respomse = await client.GetAsync(url);
            return Json(respomse);
        }

        /// <summary>
        /// 产品管理逻辑删除
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductDel(string obj,string token )
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/Product/DelProduct";
            var respomse = await client.PostAsync(url,obj);
            return Json(respomse);
        }


        /// <summary>
        /// 产品管理批量逻辑删除
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductDelRange(string token, ProductRangeModelDTO productRange)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/Product/IsDelProduct";
            string list=JsonConvert.SerializeObject(productRange);
            var respomse = await client.PostAsync(url, list);
            return Json(respomse);
        }



        //产品管理显示导出
        [HttpGet]
        public async Task<ActionResult> GEtProductExcel(string token, int index, int size, string? productNo, string? productName, string? manuNo, string? vendorId)
        {
           
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "productRead/api/ProductSelect/ProductExcel?index=" + index + "&size=" + size + "&productNo=" + productNo +
                "&productName=" + productName + "&manuNo=" + manuNo + "&vendorId=" + vendorId;
            var respomse = await client.GetAsync(url);
            return Json(respomse);
        }


        /// <summary>
        /// 产品类别逻辑删除
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeDel(string data)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/ProductType/DelProductTypes";
            var respomse = await client.PostAsync(url, data);
            return Json(respomse);
        }
        /// <summary>
        /// 产品类别添加
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GEtProductTypeAdd(string data)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/ProductType/AddProductTypes";
            var respomse = await client.PostAsync(url, data);
            return Json(respomse);
        }

        //产品类型显示
        [HttpGet]
        public async Task<ActionResult> GEtProductTypeShow(string token, int index, int size, string? productTypeNo, string? productTypeName)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "productRead/api/ProductSelect/GetProductType?index=" + index + "&size=" + size + "&productTypeNo=" + productTypeNo +
                "&productTypeName=" + productTypeName;
            var respomse = await client.GetAsync(url);
            return Json(respomse);
        }

        //产品类型显示
        [HttpGet]
        public async Task<ActionResult> GEtProductTypeFind(string token, string productTypeId)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/ProductType/FindProductTypes?productTypeId=" + productTypeId ;
            var respomse = await client.GetAsync(url);
            return Json(respomse);
        }

        /// <summary>
        /// 批量反填产品数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetProductFind(string token, int productId)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/Product/FindProduct?productId=" + productId;
            var response = await client.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 产品类别修改
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeUpdate(ProductTypeDTO obj1, string token)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/ProductType/UpProductTypes";
            string list = JsonConvert.SerializeObject(obj1);
            var respomse = await client.PostAsync(url, list);
            return Json(respomse);
        }

        /// <summary>
        /// 产品管理批量逻辑删除
        /// </summary>
        /// <param name="token"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeDelRange(string token, ProductTypeRangeModelDTO productType)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "product/api/ProductType/IsDelProduct";
            string list = JsonConvert.SerializeObject(productType);
            var respomse = await client.PostAsync(url, list);
            return Json(respomse);
        }




        public class PeoductDTO
        {
            public int ProductId { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductNo { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductName { get; set; }
            /// <summary>
            /// 厂家编码
            /// </summary>
            public string? ManuNo { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InsideNo { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            public int? UnitId { get; set; }
            /// <summary>
            /// 类别
            /// </summary>
            public int? ProductTypeId { get; set; }
            /// <summary>
            /// 上线值
            /// </summary>
            public int? GoOnline { get; set; }
            /// <summary>
            /// 下限值
            /// </summary>
            public int? Online { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? ProductSpec { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? ProductPrice { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 默认库位
            /// </summary>
            public int? BinLocationId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public string? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public string? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? ProductDesc { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public string? ProductWrap { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public string? ProductWeight { get; set; }
        }

        public class ProductTypeDTO
        {
            public int ProductTypeId { get; set; }
            /// <summary>
            /// 产品类别编号
            /// </summary>
            public string? ProductTypeNo { get; set; }
            /// <summary>
            /// 产品类别名称
            /// </summary>
            public string? ProductTypeName { get; set; }
            /// <summary>
            /// 上级类别
            /// </summary>
            public string? ProductSuperior { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? ProductRemarks { get; set; }
        }
    }
}
